package pwd.allen.springboot;

import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import pwd.allen.springboot.listener.MyListener;

import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MqDemo {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Test
    public void testSend() throws InterruptedException {
        // |String msg = "hello, Spring boot amqp";
        Map<String,String> msg = new HashMap<>();
        msg.put("name", "Jack");
        amqpTemplate.convertAndSend(MyListener.SIMPLE_QUEUE, JSON.toJSONString(msg));
        // 这里传了个map，如果配置了Jackson2JsonMessageConverter，会自动序列化成json，否则java序列化（content_type:	application/x-java-serialized-object）
        amqpTemplate.convertAndSend(MyListener.EXCHANGE,"boot.b.c", msg);

        Thread.sleep(1000L);
    }
}
